﻿using Gargoyle.Roton;
using System;
using System.Collections.Generic;
using System.Text;

namespace Gargoyle.ZZT
{
    public class ElementCollection : System.Collections.ReadOnlyCollectionBase
    {
        private byte[] elementToByte;

        public ElementCollection(Element[] elements)
        {
            InnerList.AddRange(elements);
            elementToByte = new byte[256];
            for (byte i = 0; i < elements.Length; i++)
            {
                elementToByte[(byte)elements[i].Id] = i;
            }
        }

        public byte this[Element element]
        {
            get
            {
                return elementToByte[(byte)element.Id];
            }
        }

        public Element this[byte index]
        {
            get
            {
                if (index < InnerList.Count)
                {
                    return (Element)this.InnerList[index];
                }
                return null;
            }
        }

        public Element this[ElementID id]
        {
            get
            {
                foreach (Element e in InnerList)
                {
                    if (e.Id == id)
                    {
                        return e;
                    }
                }
                return null;
            }
        }

        public byte IndexOf(ElementID id)
        {
            return elementToByte[(byte)id];
        }

        public byte IndexOf(Element e)
        {
            return elementToByte[(byte)e.Id];
        }
    }
}
